/**
 * This file is where you define your application routes and controllers.
 *
 * Start by including the middleware you want to run for every request;
 * you can attach middleware to the pre('routes') and pre('render') events.
 *
 * For simplicity, the default setup for route controllers is for each to be
 * in its own file, and we import all the files in the /routes/views directory.
 *
 * Each of these files is a route controller, and is responsible for all the
 * processing that needs to happen for the route (e.g. loading data, handling
 * form submissions, rendering the view template, etc).
 *
 * Bind each route pattern your application should respond to in the function
 * that is exported from this module, following the examples below.
 *
 * See the Express application routing documentation for more information:
 * http://expressjs.com/api.html#app.VERB
 */

var keystone = require('keystone');
var middleware = require('./middleware');
var importRoutes = keystone.importer(__dirname);
var morgan = require('morgan');
var FileStreamRotator = require('file-stream-rotator')
var fs = require('fs');
var path = require('path');
// Common Middleware
keystone.pre('routes', middleware.initLocals);
keystone.pre('render', middleware.flashMessages);

// Import Route Controllers
var routes = {
	views: importRoutes('./views'),
	api: importRoutes('./api'),
};

// Setup Route Bindings
exports = module.exports = function (app) {
	//记录错误日志
	var logDirectory = path.join(__dirname, '../logs')
	// ensure log directory exists
	fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
	var accessLogStream = FileStreamRotator.getStream({
		date_format: 'YYYYMMDD',
		filename: path.join(logDirectory, 'access-%DATE%.log'),
		frequency: 'daily',
		verbose: false
	})
	var errorLogStream = FileStreamRotator.getStream({
		date_format: 'YYYYMMDD',
		filename: path.join(logDirectory, 'error-%DATE%.log'),
		frequency: 'daily',
		verbose: false
	});
	app.use(morgan('combined', {
		skip: function (req, res) { return res.statusCode < 400 },
		stream: accessLogStream
	}))
	if (process.env.NODE_ENV === 'development') {
		var webpack = require('webpack');
		var webpackConfig = require('../webpack.config');
		// console.log('webpackConfig',webpackConfig);
		var compiler = webpack(webpackConfig);
		app.use(require('webpack-dev-middleware')(compiler, {
			publicPath: files.cdnPath,
			quiet: true,
			noInfo: true,
			stats: {colors: true, progress: true}
		}));
		app.use(require('webpack-hot-middleware')(compiler));
	}
	
	// Views
	app.get('/', middleware.requireUserSystem, function (req, res) {
        res.redirect('/userCenter/workBench.html')
    });
	//设置crsf
	app.get('/*',  middleware.csrfSet);

	app.get('/login/login.html?:returnUrl', routes.views.login.login);
	app.get('/blog/:category?', routes.views.blog);
	app.get('/blog/post/:post', routes.views.post);
	app.get('/gallery', routes.views.gallery);
	app.all('/contact', routes.views.contact);


	//userCenter
	app.get('/userCenter/workBench.html', routes.views.userCenter.workBench);
	app.get('/userCenter/userManage.html', routes.views.userCenter.userManage);
	app.get('/userCenter/createUser.html', routes.views.userCenter.createUser);
	app.get('/userCenter/userDetail.html', routes.views.userCenter.userDetail);
	app.get('/userCenter/usergroup.html', routes.views.userCenter.userGroup);
	app.get('/userCenter/system.html', routes.views.blankPage);
	
	//login(注册)
	app.get('/login/activation.html', routes.views.login.activation);

	//验证所有POST请求crsf
	app.all('/api/*', middleware.crsfCheck);

	//公用方法
	app.get('/common/captcha/', routes.api.common.captcha);
	//上传图片文件
	app.all('/common/uploadImg', routes.api.common.uploadImg);

	//API接口设置
	app.all('/api/login', middleware.captcha, routes.api.login.index);
	app.all('/api/logout', routes.api.logout.index);

	//用户
	app.all('/api/user/info', middleware.requireUserSystem, routes.api.user.info);
	app.all('/api/user/list', middleware.requireUserSystem, routes.api.user.list);
	app.post('/api/user/detailed', middleware.requireUserSystem, routes.api.user.detail);
	app.post('/api/user/update', middleware.requireUserSystem, routes.api.user.update);
	app.post('/api/user/create', middleware.requireUserSystem, routes.api.user.create);
	app.post('/api/user/delete', middleware.requireUserSystem, routes.api.user.delete);
	app.post('/api/user/changePw', middleware.requireUserSystem, routes.api.user.changePw);

	//用户角色
	app.all('/api/userGroup/list', middleware.requireUserSystem, routes.api.userGroup.list);
	app.all('/api/userGroup/detail', middleware.requireUserSystem, routes.api.userGroup.detail);
	app.all('/api/userGroup/update', middleware.requireUserSystem, routes.api.userGroup.update);
	app.all('/api/userGroup/create', middleware.requireUserSystem, routes.api.userGroup.create);
	app.all('/api/userGroup/delete', middleware.requireUserSystem, routes.api.userGroup.delete);
	
    //用户权限
    app.all('/api/authority/list', middleware.requireUserSystem, routes.api.authority.list);
    app.all('/api/authority/detail', middleware.requireUserSystem, routes.api.authority.detail);
    app.all('/api/authority/update', middleware.requireUserSystem, routes.api.authority.update);
    app.all('/api/authority/create', middleware.requireUserSystem, routes.api.authority.create);
    app.all('/api/authority/delete', middleware.requireUserSystem, routes.api.authority.delete);
    app.all('/api/authority/tree', middleware.requireUserSystem, routes.api.authority.tree);
    
	//系统参数
	app.all('/api/systemConf/list', middleware.requireUserSystem, routes.api.systemConf.list);
	app.all('/api/systemConf/detail', middleware.requireUserSystem, routes.api.systemConf.detail);
	app.all('/api/systemConf/update', middleware.requireUserSystem, routes.api.systemConf.update);
	app.all('/api/systemConf/create', middleware.requireUserSystem, routes.api.systemConf.create);
	app.all('/api/systemConf/delete', middleware.requireUserSystem, routes.api.systemConf.delete);
	// NOTE: To protect a route so that only admins can see it, use the requireUser middleware:
	// app.get('/protected', middleware.requireUser, routes.views.protected);

};
